﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using DAL;

namespace QuanLyBilliards_Coffee
{
    public partial class FormCapNhatban : Form
    {
        public FormCapNhatban()
        {
            InitializeComponent();
        }

        private void btnThoatCNBan_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormCapNhatban_Load(object sender, EventArgs e)
        {

            if (Form1.hamkt == 1)
            {
                txtmaBanCN.Text = Form1.maban;
                txtGiaTienBanCN.Visible = false;
                lblGiaBanCapNhat.Visible = false;
            }
            if (Form1.hamkt == 2)
            {
                txtmaBanCN.Text = Form1.maban;
                txtGiaTienBanCN.Visible = true;
                lblGiaBanCapNhat.Visible = true;
            }
        }

        private void btnCapNhatBan_Click(object sender, EventArgs e)
        {
            string ten = null;
            BanRepository ban=new BanRepository();
            Ban a = new Ban();
                Ban b = ban.GetByTenBan(txtmaBanCN.Text);
                string s = txtmaBanCN.Text;
                if (b.LoaiBan == "Vip")
                {
                    ten = s.Substring(0, s.Length - 3);
                }
                else
                {
                    ten = s.Substring(0, s.Length - 2);
                }
                if (ban.Delete(b))
                {
                    if (Form1.hamkt == 1)
                    {
                        a.Cafe = true;
                       
                        
                        a.Gia = 0; ;
                        if (chkBinhThuong.Checked)
                        {
                            a.LoaiBan = "Bình thường";
                            a.TenBan = ten + "BT";
                        }
                        else
                        {
                            a.LoaiBan = "Vip";
                            a.TenBan = ten + "Vip";
                        }
                        if (rdTTBanBanCN.Checked)
                        {
                            a.TrangThai = true;
                        }
                        else
                        {
                            a.TrangThai = false;
                        }
                        if (ban.AddNew(a))
                        {
                            MessageBox.Show("Thành công");
                        }
                        else
                        {
                            MessageBox.Show("Lỗi");
                        }
                    }
                    else
                        if (Form1.hamkt == 2)
                        {
                            a.Cafe = false;
                            a.TenBan = txtmaBanCN.Text;

                            if (XuLyChuoiVao.KiemTraSo(txtGiaTienBanCN.Text) == false)
                            {
                                MessageBox.Show("Giá tiền phải nhập số");
                            }
                            else
                            {
                                a.Gia =int.Parse(txtGiaTienBanCN.Text);
                                if (chkBinhThuong.Checked)
                                {
                                    a.LoaiBan = "Bình thường";
                                    a.TenBan = ten + "BT";
                                }
                                else
                                {
                                    a.LoaiBan = "Vip";
                                    a.TenBan = ten + "Vip";
                                }
                                if (rdTTBanBanCN.Checked)
                                {
                                    a.TrangThai = false;
                                }
                                else
                                {
                                    a.TrangThai = true;
                                }
                                if (ban.AddNew(a))
                                {
                                    MessageBox.Show("Thành công");
                                }
                                else
                                {
                                    MessageBox.Show("Lỗi");
                                }
                            }
                        }
                }
                else
                {
                    MessageBox.Show("Thát bại");
                }

        }
    }
}
